<?php
 
/**
* The module of assets (functions)
* Модуль активов (функции)
* @author IT studio IRBIS-team
* @copyright © 2009 IRBIS-team
*/
/////////////////////////////////////////////////////////

/**
* Generation of page of an error at access out of system
* Генерация страницы ошибки при доступе вне системы
*/
    if(!defined('IRB_KEY'))
    {
       header("HTTP/1.1 404 Not Found");      
       exit(file_get_contents('../../404.html'));
    } 
//////////////////////////////////////////////////////////
   
/**  
* Function of converting tags array to line
* Функция конвертирования массива тегов в строку через запятую
* return string  
*/  
function tags_to_line($tags_array, $link=0) 
{
	$str='';
	if(!empty($tags_array))
		{
		if($link)
			{
			foreach($tags_array as $key => $tag)
				{
				$str.="<a href=\"\">".$tag['t_caption']."</a>, ";
				}
			}
		else
			{
			foreach($tags_array as $key => $tag)
				{
				$str.=$tag['t_caption'].", ";
				}
			}
		}
	return $str;
}

function line_to_tags_array($line)
{
	if(!empty($line))
		{
			$line = htmlChars($line);
			$line = trim($line," \t,");	
			$arr = explode(",",$line);
			return strTrim($arr);
		}
	else
		return 0;
}
	
function getAsset($asset_id)
{
	if(!empty($asset_id))
	{
		$res = mysqlQuery("SELECT a.id,a.caption,a.owner,a.description,
									u.id as o_id,
									u.name as o_name, 
									u.surname as o_sname 
							FROM  `". IRB_DBPREFIX ."assets` a,
								`". IRB_DBPREFIX ."user` u
							WHERE a.id='".$asset_id."'
							AND a.owner=u.id");
					
		if(mysql_num_rows($res) > 0)
			return htmlChars(mysql_fetch_assoc($res));
			
	}
	return 0;
}	

function getAssetTags($asset_id)
{	
	if(!empty($asset_id))
	{	
		$res_t = mysqlQuery("SELECT at.id as t_id,
								at.caption as t_caption
							FROM `". IRB_DBPREFIX ."assets_tags` at
							LEFT JOIN `". IRB_DBPREFIX ."asset_link_tag` alt
							ON (at.id=alt.tag_id)
							WHERE alt.asset_id='".$asset_id."'");
		
		if(mysql_num_rows($res_t) > 0)
			{
				while($row_t=mysql_fetch_assoc($res_t))
				$a_t[] = htmlChars($row_t);
				return $a_t;
			}
	}
	return 0;
}